package com.dp.appi.controller;

import com.dp.appi.exception.CommonException;
import com.dp.appi.service.ExpCodesetService;
import com.dp.appi.service.ExplorationService;
import com.dp.appi.util.ExceptionUtil;
import com.dp.appi.util.SYS;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Map;

/**
 * Created by xiaoji on 2016/5/13.
 * 典设材料
 */
@Controller
@RequestMapping(value = "/expCodesetControl")
public class ExpCodesetControl {
    private static Logger log = LoggerFactory.getLogger(ExpCodesetControl.class);
    @Autowired
    private ExpCodesetService expCodesetService;
    @Autowired
    private ExplorationService explorationService;
    /**
     * 保存踏勘典设方案
     *
     * @param modelMap
     * @param param
     * @param exploration_id 踏勘id
     * @param codeset_id     典设方案id
     */
    @RequestMapping(value = "/saveExpCodeset", method = RequestMethod.POST)
    public void saveExpCodeset(ModelMap modelMap, @RequestParam Map<String, String> param, Long exploration_id, Long codeset_id) {
        try {
            if (codeset_id == null || "".equals(codeset_id) || null == exploration_id || "".equals(exploration_id)) {
                throw new CommonException("EXPO016");
            }

            //TODO 参数名乱写都成功
            //TODO 判断踏勘id是否为当前登陆人所有
            //TODO 判断典设id是否存在
            //TODO 判断踏勘id是否存在
            //TODO 添加前未删除踏勘id下的典设材料

            expCodesetService.saveExpCodeset(codeset_id, exploration_id);

            explorationService.upCodeSet(Long.valueOf(String.valueOf(codeset_id)),exploration_id);


            expCodesetService.updateExpCodeset(exploration_id);
            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
        } catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        } catch (Exception e) {
            String str = ExceptionUtil.getExceptionAllinformation(e);
            if (str.indexOf("SQLErrorCodeSQLExceptionTranslator") != -1) {
                modelMap.addAllAttributes(SYS.EXPO004.msgMap());
            } else {
                modelMap.addAllAttributes(SYS.E10002.msgMap());
            }
            log.error(str);
            e.printStackTrace();
        }
    }

    /**
     * 踏勘典设导入模块
     *
     * @param modelMap
     * @param param          其他参数
     * @param exploration_id 踏勘id
     * @param module_id      模块id
     */
    @RequestMapping(value = "/impExpModule", method = RequestMethod.POST)
    public void impExpModule(ModelMap modelMap, @RequestParam Map<String, String> param, Long exploration_id, String module_id) {
        try {
            String[] arr = module_id.split("\\|");
            for (int i = 0; i < arr.length; i++) {
                expCodesetService.impExpModule(Long.parseLong(arr[i]), exploration_id);
            }
            expCodesetService.updateExpCodeset(exploration_id);
            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            e.printStackTrace();
        }
    }

    /**
     * 踏勘典设导入材料
     *
     * @param modelMap
     * @param param
     * @param exploration_id 踏勘id
     * @param material_id    材料id
     */
    @RequestMapping(value = "/impExpMaterial", method = RequestMethod.POST)
    public void impExpMaterial(ModelMap modelMap, @RequestParam Map<String, String> param, Long exploration_id, String material_id) {
        try {
            String[] arr = material_id.split("\\|");
            for (int i = 0; i < arr.length; i++) {
                expCodesetService.impExpMaterial(Long.parseLong(arr[i]), exploration_id);
            }
            expCodesetService.updateExpCodeset(exploration_id);
            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            e.printStackTrace();
        }
    }

    /**
     * 修改踏勘典设材料数量
     *
     * @param modelMap
     * @param param
     * @param id       模块id或者材料id
     * @param num      数量
     * @param use_type 利旧 新建
     */
    @RequestMapping(value = "/upExpMaterial", method = RequestMethod.POST)
    public void upExpMaterial(ModelMap modelMap, @RequestParam Map<String, String> param, Long id, Integer num, Integer use_type) {
        try {
            Long exploration_id = expCodesetService.upExpMaterial(id, num, use_type);
            if (exploration_id != null) {
                expCodesetService.updateExpCodeset(exploration_id);
            }
            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
        } catch (Exception e) {
            String str = ExceptionUtil.getExceptionAllinformation(e);
            if (str.indexOf("SQLErrorCodeSQLExceptionTranslator") != -1) {
                modelMap.addAllAttributes(SYS.EXPO006.msgMap());
            } else {
                modelMap.addAllAttributes(SYS.E10002.msgMap());
            }
            log.error(str);
            e.printStackTrace();
        }
    }

    /**
     * 删除踏勘典设材料
     *
     * @param modelMap
     * @param param
     * @param id       模块id或者材料id
     */
    @RequestMapping(value = "/delExpMaterial", method = RequestMethod.POST)
    public void delExpMaterial(ModelMap modelMap, @RequestParam Map<String, String> param, Long id) {
        try {
            Long exploration_id = expCodesetService.delExpMaterial(id);
            if (exploration_id != null) {
                expCodesetService.updateExpCodeset(exploration_id);
            }
            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
        } catch (Exception e) {
            String str = ExceptionUtil.getExceptionAllinformation(e);
            if (str.indexOf("SQLErrorCodeSQLExceptionTranslator") != -1) {
                modelMap.addAllAttributes(SYS.EXPO006.msgMap());
            } else {
                modelMap.addAllAttributes(SYS.E10002.msgMap());
            }
            log.error(str);
            e.printStackTrace();
        }
    }

    /**
     *  变更踏勘点材料或模块
     *
     * @param modelMap
     * @param param
     */
    @RequestMapping(value = "/upCodesetMaterial", method = RequestMethod.POST)
    public void upCodesetMaterial(ModelMap modelMap, @RequestParam Map<String, String> param) {
        try {
            expCodesetService.upCodesetMaterial(param);
            modelMap.addAllAttributes(SYS.SUCCESS.msgMap());
        } catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            e.printStackTrace();
        }
    }

    @RequestMapping(value = "/queryExpCodeset", method = RequestMethod.GET)
    public void queryExpCodeset(ModelMap modelMap, @RequestParam Map<String, String> param, Long exploration_id) {
        try {
            modelMap.addAttribute("expCodesetList", expCodesetService.queryExpCodeset(exploration_id));
        } catch (CommonException c) {
            modelMap.addAllAttributes(SYS.valueOf(c.getMessage()).msgMap());
        } catch (Exception e) {
            modelMap.addAllAttributes(SYS.E10002.msgMap());
            e.printStackTrace();
        }
    }
}
